//
//  ByViewOrderCommitToolBar.m
//  itboye_shop_ios
//
//  Created by pro on 17/2/9.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewOrderCommitToolBar.h"

@implementation ByViewOrderCommitToolBar

- (void)dealloc {
    
    [self removeObserver:self forKeyPath:@"totalPrice"];
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        [self setupUI];
        // 添加KVO
        [self addObserver:self forKeyPath:@"totalPrice" options:NSKeyValueObservingOptionNew context:nil];
        
    }
    return self;
}

#pragma mark - KVO事件通知方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    
       if ([keyPath isEqualToString:@"totalPrice"]) {
        
           CGFloat totalPrice = [change[NSKeyValueChangeNewKey] floatValue];
           UILabel *totalPriceLabel = [self viewWithTag:222];
           totalPriceLabel.text = [NSString stringWithFormat:@"合计：￥%.2lf", totalPrice];
        
           NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:totalPriceLabel.text];
           NSRange maohaoRange = [totalPriceLabel.text rangeOfString:@"："];
           NSRange priceRange = NSMakeRange(maohaoRange.location + 1, totalPriceLabel.text.length - maohaoRange.location - 1);
           NSRange integeRange = NSMakeRange(priceRange.location + 1, priceRange.length - 4);
           [attr addAttribute:NSForegroundColorAttributeName value:KShopCarThemeColor range:priceRange];
           [attr addAttribute:NSFontAttributeName value:FONT(20) range:integeRange];
           
           totalPriceLabel.attributedText = attr;
        
    }
}

#pragma mark - 创建UI
- (void)setupUI {
    
    // 提交订单按钮
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    commitBtn.frame = CGRectMake(0, 0, self.width / 4, self.height);
    commitBtn.right = self.width;
    commitBtn.backgroundColor = KShopCarThemeColor;
    commitBtn.titleLabel.font = FONT(15);
    [commitBtn setTitle:@"提交订单" forState:UIControlStateNormal];
    [commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [commitBtn addTarget:self action:@selector(commit) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:commitBtn];
    
    // 合计label
    UILabel *totalPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width - commitBtn.width, self.height)];
    totalPriceLabel.adjustsFontSizeToFitWidth = YES;
    totalPriceLabel.font = FONT(14);
    totalPriceLabel.text = @"合计：￥0.0";
    totalPriceLabel.textColor = [UIColor blackColor];
    totalPriceLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:totalPriceLabel];
    totalPriceLabel.tag = 222;
    
    self.totalPrice = 0.00;
    
//    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:totalPriceLabel.text];
//    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:[totalPriceLabel.text rangeOfString:@"合计："]];
//    [attrStr addAttribute:NSFontAttributeName value:FONT(14) range:[totalPriceLabel.text rangeOfString:@"￥"]];
//    totalPriceLabel.attributedText = attrStr;
}



#pragma mark - 点击事件
// 提交订单
- (void)commit {
    
    if (self.commitBlock) {
        self.commitBlock();
    }
}



@end
